// This page provides functionality to delete/edit the hospitals
// Created on : may 06,06
// Author : Rupinder Saini

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using SiteManager;

public partial class Admin_ad_ManageHospital : System.Web.UI.Page
{ 
    // creating an instance of manager class
    Manager mgr = new Manager(true,"ConnectionString");
    protected void Page_Load(object sender, EventArgs e)
    {
        lblMsg.Text = "";
        if (!Page.IsPostBack)
        {
            grdmedsch.Visible = true;
            BindGridView();
        }
        foreach (GridViewRow rv in grdmedsch.Rows)
        {
            CheckBox chkRow = (CheckBox)rv.FindControl("RowLevelCheckBox");
            chkRow.Attributes.Add("onclick", "ChangeHeaderAsNeeded();");
            ClientScript.RegisterArrayDeclaration("CheckBoxIDs", String.Concat("'", chkRow.ClientID, "'"));
        }
        lbltotHosp.Text = TotalHospital().ToString();
    }

    private int TotalHospital()
    {
        int CountRec;
        if (txtName.Text.ToString().Trim() == "")
        {
            string sql = "Select Count(id)  from tbl_hospitals where isdelete=0";
            CountRec = (int)mgr.ExecuteScalarQuery(sql);
        }
        else
        {
            string sql = "select count(*) from tbl_hospitals where HName Like('%'+'" + txtName.Text.Trim() + "' + '%') and isdelete=0";
            CountRec = (int)mgr.ExecuteScalarQuery(sql);
        }
            return CountRec;
    }
    //paging
    protected void grdmedsch_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grdmedsch.PageIndex = e.NewPageIndex;
        BindGridView();
    }

    // delete confirmation
    protected void grdmedsch_RowDataBound(object sender, GridViewRowEventArgs e)
    {

        //---------------------------------------------------------------
        if (e.Row.RowType == DataControlRowType.Header)
        {
            CheckBox chkBox = (CheckBox)e.Row.FindControl("HeaderLevelCheckBox");
            chkBox.Attributes.Add("onclick", "ChangeAllCheckBoxStates(this.checked);");
        }

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            try
            {
                LinkButton lnk = (LinkButton)e.Row.FindControl("lnkDelete");
                lnk.Attributes.Add("onclick", "return confirm('Are you sure to delete this record?')");
            }
            catch (Exception es)
            {
                Response.Write(es.ToString());
            }
        }

        //---------------------------------------------------------------
        
    }

    // deleting the record
    protected void grdmedsch_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        try
        {
            int id =Convert.ToInt32(grdmedsch.DataKeys[e.RowIndex].Value.ToString());
            SqlParameter[] sqlParam = new SqlParameter[1];
            sqlParam[0] = new SqlParameter("@id", SqlDbType.Int);
            sqlParam[0].Value = id;
            mgr.ExecuteUpdate("delete_hospital", sqlParam);
            BindGridView();
            lbltotHosp.Text = TotalHospital().ToString();
        }
        catch (Exception ex)
        {
            Response.Write(ex.ToString());
        }
    }

    #region general functions

    /// <summary>
    /// binds the grid view 
    /// </summary>
    
    public void BindGridView()
    {
        try
        {
            SqlParameter[] sqlParam = new SqlParameter[1];

                sqlParam[0] = new SqlParameter("@HName", SqlDbType.VarChar);
                //sqlParam[0].Size = 500;
                sqlParam[0].Value = txtName.Text.Trim();
            
            DataSet ds = mgr.ExecuteQuery("[dbo].[get_hospitals]", sqlParam, "tblHospital");
            if (ds.Tables[0].Rows.Count > 0)
            {
                grdmedsch.DataSource = ds;
                grdmedsch.PageSize = Convert.ToInt32(Session["AdminPageSize"].ToString()); 
                grdmedsch.DataBind();
                //added by ranjeet at 29/02/2008
        //        TotalSearchHospital();
            }
            else
            {

                lblMsg.Text = "No Records found!!<br>Please click the Add Hospital link on the left panel to add a new hospital.";
                lblMsg.ForeColor = System.Drawing.Color.Red;
                grdmedsch.Visible = false;
                lblMsg.Visible = true;
            }
        }
        catch (Exception ex)
        {
            Response.Write(ex.ToString());
        }
    }

    #endregion

    protected void butSerach_Click(object sender, EventArgs e)
    {
        grdmedsch.Visible = true;
        BindGridView();
    }
    protected void grdmedsch_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Del")
        {
            foreach (GridViewRow rv in grdmedsch.Rows)
            {
                CheckBox chk = (CheckBox)rv.FindControl("RowLevelCheckBox");
                if (chk.Checked)
                {
                    int id = Convert.ToInt32(grdmedsch.DataKeys[rv.RowIndex].Value.ToString());
                    SqlParameter[] sqlParam = new SqlParameter[1];
                    sqlParam[0] = new SqlParameter("@id", SqlDbType.Int);
                    sqlParam[0].Value = id;
                    mgr.ExecuteUpdate("delete_hospital", sqlParam);
                }
            }
            BindGridView();
            lbltotHosp.Text = TotalHospital().ToString();
        }
    }
    //ADDED BY RANJEET TO DISPLY TOTAL HOSPITAL SEARCH CRITERIA  WISE
    private int TotalSearchHospital()
    {
        string sql = "select count(*) from tbl_hospitals where HName Like('%'+'" + txtName.Text.Trim() + "' + '%') and isdelete=0";
        int CountRec = (int)mgr.ExecuteScalarQuery(sql);
        return CountRec;
    }
}
